import { BrowserWindow, shell } from 'electron';
import path from 'node:path';
import { is } from '@electron-toolkit/utils';
import icon from '../../resources/icon.png?asset';

// 浏览器窗口的配置项
const BROWSER_WINDOWS_OPTIONS = {
  // 设计器主窗口
  MAIN: {
    options: {
      width: 1160,
      height: 752,
      minHeight: 632,
      minWidth: 960,
      show: false,
      frame: false,
      title: '设计器主窗口',
      autoHideMenuBar: true,
      webPreferences: {
        preload: path.join(__dirname, '../preload/index.js'),
        sandbox: false
      },
      ...(process.platform === 'linux' ? { icon } : {}),
    },
    entry: {
      file: path.join(__dirname, '../renderer/designer.html'),
      url: `${process.env['ELECTRON_RENDERER_URL']}/designer.html`
    }
  },
  // 录制指示器
  RECORDER: {
    options: {
      width: 400,
      height: 200,
      minHeight: 400,
      minWidth: 200,
      show: false,
      frame: false,
      title: '录制指示器',
      autoHideMenuBar: true,
      webPreferences: {
        preload: path.join(__dirname, '../preload/index.js'),
        sandbox: false
      },
      ...(process.platform === 'linux' ? { icon } : {}),
    },
    entry: {
      file: path.join(__dirname, '../renderer/recorder.html'),
      url: `${process.env['ELECTRON_RENDERER_URL']}/recorder.html`
    }
  }
}

type Identifier = keyof typeof BROWSER_WINDOWS_OPTIONS

// 浏览器窗口管理
class BrowserWindowManager {

  browserWindows: Map<string, any> = new Map();

  // 创建浏览器窗口
  createBrowserWindow(key: Identifier) {

    const config = BROWSER_WINDOWS_OPTIONS[key];

    if (this.browserWindows.has(key)) {
      return
    }

    const browserWindow = new BrowserWindow(config.options)

    browserWindow.once('ready-to-show', () => {
      browserWindow.show()
    })

    browserWindow.webContents.setWindowOpenHandler((details) => {
      shell.openExternal(details.url)
      return { action: 'deny' }
    })
    if (is.dev) {
      browserWindow.loadURL(config.entry.url)
    } else {
      browserWindow.loadFile(config.entry.file)
    }
    this.browserWindows.set(key, browserWindow)
    return browserWindow;
  }

  // 关闭浏览器窗口
  closeBrowserWindow(browserWindow) {
    for (let [key, value] of this.browserWindows) {
      if (value === browserWindow) {
        this.browserWindows.delete(key)
        break
      }
    }
    browserWindow.close()
  }

  // 浏览器窗口是否存在
  browserWindowIsExist(key: Identifier) {
    const browserWindow = this.browserWindows.get(key)
    return browserWindow && !browserWindow.isDestroyed()
  }

  // 获取浏览器窗口
  getBrowserWindow(key: Identifier) {
    return this.browserWindows.get(key)
  }

  // 广播到所有窗口
  broadcastToAllWindows(channel, data) {
    this.browserWindows.forEach((browserWindow) => {
      browserWindow.webContents.send(channel, data);
    });
  };

  // 广播到窗口
  broadcastToWindow(identifier, channel, data) {
    this.browserWindows.get(identifier)?.webContents.send(channel, data);
  };

}

// 导出单例
export const browserWindowManager = new BrowserWindowManager()
